const {
  WORD_ORDER_OPERATION_TYPE,
  SERVICE_MODE
} = require('../../../constant/index')

const {
  updateWorkOrder
} = require('../../../api/workOrder')
const {
  queryFaultTypeOptions
} = require('../../../api/faultType')
const {
  queryMaintenanceModeOptions
} = require('../../../api/maintenanceMode')
const {
  to,
  openLoading,
  closeLoading,
  showToast,
  backParentLoad,
  getEventData
} = getApp()
Page({

  data: {
    operationOptions: [{
        name: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_TELEPHONE_SOLUTION,
        text: '电话解决'
      },
      {
        name: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_REMOTE_RESOLUTION,
        text: '远程解决'
      },
    ],
    formData: {
      operationType: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_TELEPHONE_SOLUTION
    }
  },

  onLoad() {
    this.setData({
      orderData: JSON.parse(this.options.data)
    })
    this.getFaultTypeOptions();
  },

  onCustomTypeChange(event) {
    const {
      type
    } = getEventData(event);

    this.setData({
      [`formData.${type}`]: event.detail
    })

    this.clearForm(type);

    if (event.detail || event.detail === 0) {
      if (type === 'faultTypeId') {
        this.queryMaintenanceModeOptions();
      }
    }
  },

  clearForm(type) {
    let clearList = [];
    let optionList = [];

    if (type === 'faultTypeId') {
      clearList = ['maintenanceModeId']
      optionList = ['maintenanceModeOptions']
    }

    for (const item of clearList) {
      this.setData({
        [`formData.${item}`]: ''
      })
    }

    for (const item of optionList) {
      this.setData({
        [item]: ''
      })
    }
  },

  async getFaultTypeOptions() {
    const params = {
      assetsTypeId: this.data.orderData.assetsTypeId,
      serviceMode: SERVICE_MODE.repair
    }
    const [err, data] = await to(queryFaultTypeOptions(params))

    if (!err) {
      this.setData({
        faultTypeOption: data
      })
    }
  },

  async queryMaintenanceModeOptions() {
    const params = {
      faultTypeId: this.data.formData.faultTypeId
    }
    openLoading()
    const [err, data] = await to(queryMaintenanceModeOptions(params))
    closeLoading()

    if (!err) {
      this.setData({
        maintenanceModeOptions: data
      })
    }
  },

  checkValidate() {
    const {
      faultTypeId,
      maintenanceModeId,
      operationType,
      operationContent
    } = this.data.formData

    if (!faultTypeId) {
      showToast('请选择故障事件')
      return false
    }

    if (!maintenanceModeId) {
      showToast('请选择维护方式')
      return false
    }

    if (operationType === WORD_ORDER_OPERATION_TYPE.WORK_ORDER_NOT_FAULT ) {
      if (!operationContent) {
        showToast('请输入事件解决说明')
        return false;
      }
    }
    return this.data.formData;
  },

  handleSubmit() {
    const result = this.checkValidate();

    if (!result) {
      return;
    }
    const {
      operationType
    } = result;
    const params = {
      operationType: operationType,
      operationContent: result.operationContent,
      appointmentTime: result.appointmentTime,
      faultTypeId: result.faultTypeId,
      maintenanceModeId: result.maintenanceModeId,
      workOrderId: this.data.orderData.workOrderId,
      comments: result.comments
    }

    this.submitPromise(params);
  },

  async submitPromise(params) {
    openLoading('提交中')
    const [err] = await to(updateWorkOrder(params));
    closeLoading()
    if (!err) {
      showToast({
        message: '提交成功',
        type: 'success',
        mask: true,
        onClose: () => {
          setTimeout(() => {
            backParentLoad(1)
          }, 1000)
        }
      })
    }
  }
})